﻿CREATE FUNCTION [util].[IsTimeInScheduleScope]
(@ScheduleTime DATETIME, @ScheduleScope BINARY (28))
RETURNS BIT
AS
BEGIN
	DECLARE @DayHourScope binary(4)
	SET @DayHourScope = SUBSTRING(@ScheduleScope, ((DATEPART(WeekDay,@ScheduleTime) - 1) * 4) + 1, 4)

	DECLARE @ReturnValue bit

	SET @ReturnValue = CASE
			WHEN (CAST(@DayHourScope AS int) & POWER(2, DATEPART(hour,@ScheduleTime))) <> 0 THEN 1
			ELSE 0
		END

	RETURN(@ReturnValue)
END